import { getDatesInRange, getSpecialDayBaseToday } from "./getDate";
import { getReportBugsByDate } from "./getReportBugs";

export const migrateReport = () => {
  const days = getDatesInRange(getSpecialDayBaseToday(1), 14);
  
  for (let i = 0; i < days.length; i++) {
    const day = days[i];
    const report = getReportBugsByDate(day) as any;

    if(report && !Array.isArray(report) && report.todayReport) {
      const newFormatBugs = report.todayReport.map((bug: string) => {
        const bugId = /\s+\d+/.exec(bug)?.[0] as string;
        const bugName = /Bug\s+\d+\s+([^()]+)\s+\(/.exec(bug)?.[1].trim() as string;
        const bugUrl = /\((http:\/\/.+?)\)/.exec(bug)?.[1] as string;

        return {
          id: bugId,
          name: bugName,
          url: bugUrl,
        };
      });

      localStorage.removeItem(day);
      localStorage.setItem(day, JSON.stringify(newFormatBugs));
    }
  }
}